﻿/*
 * Copyright 2009 José David Tuero Rodríguez

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 * */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;

namespace AtmosCreator
{
    /// <summary>
    /// Business class for configuration control
    /// </summary>
    /// 
    class Configuration
    {
        /// <summary>
        /// Instance of the configuration file
        /// </summary>
        private ConfigurationFile _file;

        /// <summary>
        /// Game to use
        /// </summary>
        private string _game;

        /// <summary>
        /// Constructor
        /// </summary>
        ///  /// <param name="game">Game used</param>
        public Configuration(string game)
        {
            _game = game;
            DeserializeConf();
        }

        /// <summary>
        /// Save the configuration to hard disk
        /// </summary>
        public void Save()
        {
            XmlSerializer serializer = new XmlSerializer(typeof(ConfigurationFile));
            StreamWriter writer = new StreamWriter(AtmosCreator.Properties.Resources.GamesDirectory + "\\" + _game + "\\" + AtmosCreator.Properties.Resources.ConfFilename);
            serializer.Serialize(writer, _file);
            writer.Close();
        }

        /// <summary>
        /// Deserialize the configuration file
        /// </summary>
        private void DeserializeConf()
        {
            FileStream fileStream = null;
            XmlSerializer serializer = new XmlSerializer(typeof(ConfigurationFile));
            try
            {
                fileStream = new FileStream(AtmosCreator.Properties.Resources.GamesDirectory + "\\" +  _game + "\\" + AtmosCreator.Properties.Resources.ConfFilename, FileMode.Open);
                _file = (ConfigurationFile)serializer.Deserialize(fileStream);
            }
            finally
            {
                fileStream.Close();
            }
        }

        /// <summary>
        /// Creates a default configuration file
        /// </summary>
        public static void CreateDefaultConfig(string game)
        {
           ConfigurationFile conf = new ConfigurationFile();
           conf.VidPer = int.Parse(AtmosCreator.Properties.Resources.DefaultVidPer);
           conf.VidSeconds = int.Parse(AtmosCreator.Properties.Resources.DefaultVidSec);
           conf.Stretch = bool.Parse(AtmosCreator.Properties.Resources.DefaultStretch);
           conf.GameTime = int.Parse(AtmosCreator.Properties.Resources.DefaultPlayTime);
           conf.MinChosen = int.Parse(AtmosCreator.Properties.Resources.DefaultMinChosen);
           conf.MaxChosen = int.Parse(AtmosCreator.Properties.Resources.DefaultMaxChosen);
           conf.PerChosen = int.Parse(AtmosCreator.Properties.Resources.DefaultPerChosen);
           XmlSerializer serializer = new XmlSerializer(typeof(ConfigurationFile));
           StreamWriter writer = new StreamWriter(AtmosCreator.Properties.Resources.GamesDirectory + "\\" + game + "\\" + AtmosCreator.Properties.Resources.ConfFilename);
           serializer.Serialize(writer, conf);
           writer.Close();         
        }


        /// <summary>
        /// get and set methods
        /// </summary>
        public int VidPer
        {
            get
            {
                return _file.VidPer;
            }
            set
            {
                _file.VidPer = value;
            }
        }

        /// <summary>
        /// get and set methods
        /// </summary>
        public int VidSeconds
        {
            get
            {
                return _file.VidSeconds;
            }
            set
            {
                 _file.VidSeconds = value;
            }
        }

        /// <summary>
        /// get and set methods
        /// </summary>
        public int GameTime
        {
            get
            {
                return _file.GameTime;
            }
            set
            {
                _file.GameTime = value;
            }
        }

        public bool Stretch
        {
            get
            {
                return _file.Stretch;
            }
            set
            {
                _file.Stretch = value;
            }
        }


        /// <summary>
        /// get and set methods
        /// </summary>
        public int MinChosen
        {
            get
            {
                return _file.MinChosen;
            }
            set
            {
                _file.MinChosen = value;
            }
        }

        /// <summary>
        /// get and set methods
        /// </summary>
        public int MaxChosen
        {
            get
            {
                return _file.MaxChosen;
            }
            set
            {
                _file.MaxChosen = value;
            }
        }

        /// <summary>
        /// get and set methods
        /// </summary>
        public int PerChosen
        {
            get
            {
                return _file.PerChosen;
            }
            set
            {
                _file.PerChosen = value;
            }
        }


   
    }
}
